package com.wenhai.concurrency.thread.schedule;

import com.wenhai.concurrency.thread.pool.MyCallableA;
import com.wenhai.concurrency.thread.pool.MyCallableB;

import java.util.concurrent.*;

/**
 * {@link java.util.concurrent.ScheduledThreadPoolExecutor} 延迟示例
 *
 * @author wenhai
 * @date   2021/4/29
 */
public class ScheduleThreadPoolExecutorDelayDemo {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
//        ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor();
        ScheduledThreadPoolExecutor executor = (ScheduledThreadPoolExecutor) Executors.newScheduledThreadPool(2);
        ScheduledFuture<String> scheduleA = executor.schedule(new MyCallableA(), 4L, TimeUnit.SECONDS);
        ScheduledFuture<String> scheduleB = executor.schedule(new MyCallableB(), 4L, TimeUnit.SECONDS);
        executor.setExecuteExistingDelayedTasksAfterShutdownPolicy(false);
        executor.shutdown();
        System.out.println("当前时间为：" + System.currentTimeMillis());
//        System.out.println("scheduleA 返回值为：" + scheduleA.get());
//        System.out.println("当前时间为：" + System.currentTimeMillis());
//        System.out.println("scheduleB 返回值为：" + scheduleB.get());
        System.out.println("当前时间为：" + System.currentTimeMillis());
    }
}
